import cv2
import numpy as np

def find_small_image_in_large(large_image_path, small_image_path, threshold=0.8):
    # 读取大图和小图
    large_image = cv2.imread(large_image_path)
    small_image = cv2.imread(small_image_path)

    # 将小图转换为灰度图
    small_gray = cv2.cvtColor(small_image, cv2.COLOR_BGR2GRAY)

    # 在大图中进行模板匹配
    result = cv2.matchTemplate(cv2.cvtColor(large_image, cv2.COLOR_BGR2GRAY), small_gray, cv2.TM_CCOEFF_NORMED)

    # 查找匹配的位置
    y_loc, x_loc = np.where(result >= threshold)

    # 如果找到了匹配
    if len(x_loc) > 0:
        print("找到小图！")
        # 在大图上绘制矩形框，标记出小图的位置
        # for (x, y) in zip(x_loc, y_loc):
        #     cv2.rectangle(large_image, (x, y), (x + small_image.shape[1], y + small_image.shape[0]), (0, 255, 0), 2)

        # 显示结果
        # cv2.imshow('Detected', large_image)
        # cv2.waitKey(0)
        # cv2.destroyAllWindows()
    else:
        print("没有找到小图。")
